package org.mymail.mail;

import java.io.*;
import javax.mail.Address;
import noNamespace.FreskoSubscribersDocument;
import org.apache.xmlbeans.XmlException;

public class FileUtil {

	public FileUtil() {
	}

	static boolean moveFile(File src) {
		String path = src.getParent();
		String fileName = src.getName();
		File dir = new File((new StringBuilder(String.valueOf(path))).append(
				"\\Done\\").append(fileName).toString());
		boolean s = src.renameTo(dir);
		return s;
	}

	static String readFile(String fileLoc) {
		String finalStr = "";
		File file = new File(fileLoc);
		try {
			BufferedReader in = new BufferedReader(new FileReader(file));
			String str;
			while ((str = in.readLine()) != null)
				finalStr = (new StringBuilder(String.valueOf(finalStr)))
						.append(str).toString();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return finalStr;
	}

	public static int getEmailNumber(int mailFwdtype) {
		File dest = new File(getDestFile(mailFwdtype));
		String destImages[] = dest.list();
		return destImages.length;
	}

	public static String getSrcFile(int fwdtype) {
		String src_file = "";
		switch (fwdtype) {
		case 1: // '\001'
			src_file = "C:\\TAB_FILES\\Calvin\\good";
			break;

		case 2: // '\002'
			src_file = "C:\\TAB_FILES\\Dilbert\\good";
			break;
		}
		return src_file;
	}

	public static String getDestFile(int fwdtype) {
		String dest_file = "";
		switch (fwdtype) {
		case 1: // '\001'
			dest_file = "C:\\TAB_FILES\\Calvin\\good\\Done";
			break;

		case 2: // '\002'
			dest_file = "C:\\TAB_FILES\\Dilbert\\good\\Done";
			break;
		}
		return dest_file;
	}

	public static File getFileOnID(int mailFwdType, int fileId) {
		File f = new File(getSrcFile(mailFwdType));
		String list[] = f.list();
		String fileLoc = (new StringBuilder(String
				.valueOf(getSrcFile(mailFwdType)))).append("\\").append(
				list[fileId]).toString();
		File fileToBeSent = new File(fileLoc);
		return fileToBeSent;
	}

	public static void updateLastReceived(Address address2, int mailFwdType,
			int fileId) {
		noNamespace.FreskoSubscribersDocument.FreskoSubscribers.Subscriber subscribers[] = (noNamespace.FreskoSubscribersDocument.FreskoSubscribers.Subscriber[]) null;
		FreskoSubscribersDocument recipientsDoc = null;
		try {
			recipientsDoc = noNamespace.FreskoSubscribersDocument.Factory
					.parse(new File(
							MailConstants.recipientsXMLLoc));
			noNamespace.FreskoSubscribersDocument.FreskoSubscribers s = recipientsDoc
					.getFreskoSubscribers();
			subscribers = s.getSubscriberArray();
		} catch (XmlException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		for (int i = 0; i < subscribers.length; i++)
			if (subscribers[i].getId().equalsIgnoreCase(address2.toString())) {
				noNamespace.FreskoSubscribersDocument.FreskoSubscribers.Subscriber.SubscribeType subType[] = subscribers[i]
						.getSubscribeTypeArray();
				for (int j = 0; j < subType.length; j++) {
					noNamespace.FreskoSubscribersDocument.FreskoSubscribers.Subscriber.SubscribeType subscribeType = subType[j];
					if (subscribeType.getId() != mailFwdType)
						continue;
					subscribeType.setLastReceived(fileId);
					break;
				}

			}

		try {
			recipientsDoc
					.save(new File(
							MailConstants.recipientsXMLLoc));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}